<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FreeMarker 模板引擎完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
            margin: 3rem 0;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        pre code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">FreeMarker 模板引擎</h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">基于模板生成文本输出的强大 Java 工具</p>
                <div class="flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-blur-sm">
                        <i class="fas fa-code mr-2"></i>纯 Java 编写
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-blur-sm">
                        <i class="fas fa-layer-group mr-2"></i>MVC 模式
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-blur-sm">
                        <i class="fas fa-rocket mr-2"></i>高性能
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8 md:p-12 card-hover">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-info-circle mr-3 text-purple-600"></i>
                    什么是 FreeMarker？
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    <span class="drop-cap serif-font">F</span>reeMarker 是一个模板引擎，一个基于模板生成文本输出的通用工具，使用纯 Java 编写。FreeMarker 被设计用来生成 HTML Web 页面，特别是基于 MVC 模式的应用程序。
                </p>
                <p class="text-lg leading-relaxed text-gray-700 mb-8">
                    虽然 FreeMarker 具有一些编程的能力，但通常由 Java 程序准备要显示的数据，由 FreeMarker 生成页面，通过模板显示准备的数据。
                </p>
                
                <!-- 工作原理图 -->
                <div class="bg-gray-50 rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-center">工作原理</h3>
                    <div class="mermaid">
                        graph LR
                        A[数据模型] --> C{FreeMarker<br/>模板引擎}
                        B[模板文件] --> C
                        C --> D[输出页面]
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Quick Start Section -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 text-center gradient-text">快速开始</h2>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-download"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">1. 添加依赖</h3>
                    <p class="text-gray-600">在 SpringBoot 项目中引入 FreeMarker 依赖</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-cog"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">2. 默认配置</h3>
                    <p class="text-gray-600">了解 FreeMarker 的默认配置，通常无需修改</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-file-code"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">3. 创建模板</h3>
                    <p class="text-gray-600">创建 .ftl 模板文件，编写模板内容</p>
                </div>
            </div>

            <!-- Maven 依赖代码 -->
            <div class="bg-white rounded-xl shadow-lg p-8">
                <h3 class="text-2xl font-semibold mb-4">
                    <i class="fas fa-code mr-2 text-purple-600"></i>
                    Maven 依赖配置
                </h3>
                <pre class="code-block p-6"><code>&lt;!-- 添加 FreeMarker 依赖 --&gt;
&lt;dependency&gt;   
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;   
    &lt;artifactId&gt;spring-boot-starter-freemarker&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Basic Operations Section -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 text-center gradient-text">基本操作</h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <!-- 访问对象 -->
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-user-tag mr-2 text-purple-600"></i>
                        访问对象属性
                    </h3>
                    <pre class="code-block p-4 text-sm"><code>${stu.name}
${(user.sex == 1) ? string('男','女')}</code></pre>
                </div>

                <!-- 遍历集合 -->
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-list mr-2 text-purple-600"></i>
                        遍历集合
                    </h3>
                    <pre class="code-block p-4 text-sm"><code>&lt;#list list as stu&gt;
    序号：${stu_index}
    姓名：${stu.name}
    电话：${stu.phone}
&lt;/#list&gt;</code></pre>
                </div>

                <!-- 逻辑判断 -->
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-code-branch mr-2 text-purple-600"></i>
                        逻辑判断
                    </h3>
                    <pre class="code-block p-4 text-sm"><code>&lt;#if (age &gt; 40)&gt;
    大叔级
&lt;#elseif (age &gt; 30)&gt;
    老腊肉
&lt;#else&gt;
    小鲜肉
&lt;/#if&gt;</code></pre>
                </div>

                <!-- NULL值处理 -->
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-shield-alt mr-2 text-purple-600"></i>
                        NULL 值处理
                    </h3>
                    <pre class="code-block p-4 text-sm"><code>email: ${email!'默认值'}

&lt;#if nullObject??&gt;
    该值不为 null
&lt;#else&gt;
    该值为 null
&lt;/#if&gt;</code></pre>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Static Page Generation Section -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 text-center gradient-text">生成静态页面实战</h2>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 md:p-12">
                <div class="mb-8">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-server mr-3 text-purple-600"></i>
                        Controller 实现
                    </h3>
                    <p class="text-gray-600 mb-4">以下代码展示了如何使用 FreeMarker 生成单个商品的静态页面：</p>
                    <pre class="code-block p-6 text-sm overflow-x-auto"><code>@RequestMapping("/createHtml")
public String createHtml(Integer gID, HttpServletRequest req) throws Exception {
    // 1. 根据商品ID查询对象
    Goods goods = goodsService.getById(gID);
    
    Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
    map.put("goods", goods);
    map.put("images", goods.getGimage().split("\\|"));
    map.put("contextPath", req.getContextPath() + "/");
    
    // 2. 获取静态资源文件路径
    String path = ItemController.class.getClassLoader()
                    .getResource("static").getPath();
    
    File file = new File(path + File.separator + "page");
    
    // 3. 创建页面存放路径
    if (!file.exists()) {
        file.mkdirs();
    }
    
    try {
        // 4. 获取模板
        Template template = configuration.getTemplate("goodsItem.ftl");
        
        // 5. 设置生成模板的路径
        FileWriter fileWriter = new FileWriter(
            file.getAbsolutePath() + File.separator + gID + ".html"
        );
        
        // 6. 生成模板
        template.process(map, fileWriter);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    return "";
}</code></pre>
                </div>

                <div class="grid md:grid-cols-2 gap-6 mt-8">
                    <div class="bg-gray-50 rounded-xl p-6">
                        <h4 class="text-lg font-semibold mb-3 flex items-center">
                            <i class="fas fa-link mr-2 text-purple-600"></i>
                            设置 